package com.medica.ledproject.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.medica.ledproject.common.CustomException;
import com.medica.ledproject.common.FileUtils;
import com.medica.ledproject.common.ResponseEntity;
import com.medica.ledproject.entity.*;
import com.medica.ledproject.service.IDzParkLedProgramsService;
import com.medica.ledproject.service.IDzParkLedscreeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author xiaona
 * @date 2024/5/13
 * @description TODO
 */
@CrossOrigin
@RestController
@RequestMapping("/Led")
@Tag(name = "led屏幕节目管理")
public class BXController {
    @Value("${uplodFilePath}")
    public  String uploadFile;
    @Autowired
    IDzParkLedscreeService iDzParkLedscreeService;
    @Autowired
    IDzParkLedProgramsService iDzParkLedProgramsService;



    @Operation(summary = "打开led屏幕")
    @ApiOperationSupport(order = 13)
    @PostMapping(value = "/takeBxON")
    @ResponseBody
    public ResponseEntity takeBxON( @RequestBody  RequestPro requestPro){
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(requestPro));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return ResponseEntity.ok(iDzParkLedscreeService.updateLegState(requestPro,true));

    }
    @Operation(summary = "打开led屏幕")
    @ApiOperationSupport(order = 14)
    @PostMapping(value = "/takeBxOff")
    @ResponseBody
    public ResponseEntity takeBxOff(@RequestBody   RequestPro requestPro){
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(requestPro));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return ResponseEntity.ok(iDzParkLedscreeService.updateLegState(requestPro,false));
    }
    @Operation(summary = "发布节目")
    @ApiOperationSupport(order = 15)
    @PostMapping(value = "/sendPrograms")
    @ResponseBody
    public ResponseEntity sendPrograms( @RequestBody  RequestProN requestProN){
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(requestProN));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.ok(iDzParkLedProgramsService.sendPrograms(requestProN));
    }
    @Operation(summary = "上传节目")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/uploadPrograms")
    @ResponseBody
    public ResponseEntity uploadPrograms( @RequestBody  ReDzParkLedPrograms reDzParkLedPrograms){
        System.out.println("111111111111"+reDzParkLedPrograms.getName());
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(reDzParkLedPrograms));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        DzParkLedPrograms dzParkLedPrograms=new DzParkLedPrograms();
        try {
            dzParkLedPrograms.setInfo(reDzParkLedPrograms);
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
        String m=iDzParkLedProgramsService.save(dzParkLedPrograms)?"上传成功":"上传失败";
        return ResponseEntity.ok();
    }
    @Operation(summary = "修改节目")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "/updatePrograms")
    @ResponseBody
    public ResponseEntity updatePrograms( @RequestBody ReDzParkLedPrograms reDzParkLedPrograms){
        DzParkLedPrograms dzParkLedPrograms=new DzParkLedPrograms();
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(reDzParkLedPrograms));
            dzParkLedPrograms.setInfo(reDzParkLedPrograms);
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
        return ResponseEntity.ok(iDzParkLedProgramsService.updatePrograms(dzParkLedPrograms));
    }
    @Operation(summary = "删除节目")
    @ApiOperationSupport(order = 5)
    @GetMapping(value = "/delPrograms")
    @ResponseBody
    public ResponseEntity delPrograms(@RequestParam Integer  id){

        return ResponseEntity.ok(iDzParkLedProgramsService.delPrograms(id));
    }
    @Operation(summary = "上传文件")
    @ApiOperationSupport(order = 6)
    @Parameters({
            @Parameter(name = "file", description = "文件地址", required = true),
            @Parameter(name = "type", description = "文件类型：1图片，2视频", required = false)
    })
    @PostMapping(value = "/uploadfile")
    @ResponseBody
    public ResponseEntity uploadProgram(int type, MultipartFile file){
        System.out.println("文件名"+file.getOriginalFilename());
        System.out.println("文件类型"+file.getContentType());
        System.out.println("文件大小"+file.getSize());
        String fileName ="";
        try {
            fileName = FileUtils.uploadfile(file,uploadFile,type);
        }catch (Exception e){

        }
        return ResponseEntity.ok( new Files(fileName));
    }
    @Operation(summary = "获取节目列表")
    @ApiOperationSupport(order = 7)
    @GetMapping(value = "/getPrograList")
    @ResponseBody
    public ResponseEntity getPrograList( ProgramRequestDao programRequestDao){

        return ResponseEntity.ok(iDzParkLedProgramsService.getPrograms(programRequestDao));
    }
    @Operation(summary = "添加led屏幕")
    @ApiOperationSupport(order = 8)
    @GetMapping(value = "/addScree")
    @ResponseBody
    public ResponseEntity addScree(ReDzParkLedscree reDzParkLedscree ){
        DzParkLedscree d=new DzParkLedscree();
        d.setInfo(reDzParkLedscree);
        return ResponseEntity.ok(iDzParkLedscreeService.save(d));
    }
    @Operation(summary = "修改led屏幕")
    @ApiOperationSupport(order = 9)
    @PostMapping(value = "/updateScree")
    @ResponseBody
    public ResponseEntity updateScree( @RequestBody ReDzParkLedscree reDzParkLedscree ){

        DzParkLedscree d=new DzParkLedscree();
        d.setInfo(reDzParkLedscree);
        return ResponseEntity.ok(iDzParkLedscreeService.updateLedScree(reDzParkLedscree.getId(),d));
    }
    @Operation(summary = "删除led")
    @ApiOperationSupport(order = 10)
    @GetMapping(value = "/delScree")
    @ResponseBody
    public ResponseEntity delScree( @RequestParam Integer  id ){
        DzParkLedscree d=new DzParkLedscree();
        d.setIsDel(-1);
        return ResponseEntity.ok(iDzParkLedscreeService.updateLedScree(id,d));
    }
    @Operation(summary = "批量上传led列表")
    @ApiOperationSupport(order = 11)
    @PostMapping(value = "/uploadLedList")
    @ResponseBody
    public ResponseEntity uploadLedList(@RequestParam("file") MultipartFile file){

        return ResponseEntity.ok(iDzParkLedscreeService.uploadLegList(file));
    }

    @Operation(summary = "获取led屏幕列表")
    @ApiOperationSupport(order = 12)
    @Parameters({
            @Parameter(name = "page", description = "页码", required = true),
            @Parameter(name = "pageSize", description = "每页数量", required = true),
    })
    @GetMapping(value = "/getLedScreeList")
    @ResponseBody
    public ResponseEntity getLedScreeList(@RequestParam(defaultValue = "1") int page ,@RequestParam(defaultValue = "20")int pageSiza){

        return ResponseEntity.ok(iDzParkLedscreeService.getLedList(page,pageSiza));
    }
    @Operation(summary = "获取led屏幕列表")
    @ApiOperationSupport(order = 12)
    @Parameters({
            @Parameter(name = "page", description = "页码", required = true),
            @Parameter(name = "pageSiza", description = "每页数量", required = true),
    })
    @GetMapping(value = "/getPhone")
    @ResponseBody
    public ResponseEntity getPhone(Phone phone   ){
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writeValueAsString(phone));
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
        return ResponseEntity.ok();
    }
}
